define(['app', 'ace-diff'], function(app, AceDiff) {
    app.controller('CodeComparatorController', function($scope, $compile, $timeout) {
        var self = this;
        self.hasDiff = false;

        var aceInstance = null;
        var aceDiffs = [];
        var aceDiffIdx = 0;

        self.init = function() {
            initAceDiff('', '');
            $scope.$on('$destroy', function() {
                if (aceInstance) {
                    aceInstance.destroy();
                }
            });
        }

        function initAceDiff(leftContent, rightContent) {
            if (aceInstance) {
                aceInstance.destroy();
            }
            aceInstance = new AceDiff({
                theme: 'ace/theme/chrome',
                element: '.acediff',
                diffGranularity: 'specific',
                left: {
                    content: leftContent,
                },
                right: {
                    content: rightContent,
                },
            });
            // Set editor config
            var editors = aceInstance.getEditors();
            _.each([editors.left, editors.right], function(editor) {
                editor.setOption('fontSize', 14);
                editor.setOption('wrap', true);
                editor.setOption('showPrintMargin', false);
            });
            editors.left.focus();
            // Get diff
            getAceDiff();
            function getAceDiff() {
                if (aceInstance.diffs) {
                    if (!_.isEmpty(aceInstance.diffs)) {
                        self.hasDiff = true;
                        aceDiffs = aceInstance.diffs;
                        aceDiffIdx = 0;
                    }
                } else {
                    $timeout(getAceDiff, 50);
                }
            }
        }

        self.diff = function() {
            //aceInstance.diff();
            var editors = aceInstance.getEditors();
            var leftContent = editors.left.getValue();
            var rightContent = editors.right.getValue();
            initAceDiff(leftContent, rightContent);
        }

        self.prevDiff = function() {
            if (aceDiffs.length > 0) {
                if (++aceDiffIdx >= aceDiffs.length) {
                    aceDiffIdx = 0;
                }
                var lineNumber = aceDiffs[aceDiffIdx].leftStartLine;
                aceInstance.getEditors().left.gotoLine(lineNumber);
            }
        }

        self.nextDiff = function() {
            if (aceDiffs.length > 0) {
                if (--aceDiffIdx <= -1) {
                    aceDiffIdx = aceDiffs.length - 1;
                }
                var lineNumber = aceDiffs[aceDiffIdx].leftStartLine;
                aceInstance.getEditors().left.gotoLine(lineNumber);
            }
        }

        self.reset = function() {
            initAceDiff('', '');
        }

        self.callback = function(height, width, refresh) {
            // Toolbar
            if (refresh) {
                $('.navbar-form').append($compile($('#opt-toolbar').html())($scope));
            }
        }
    });
});
